<% layout('/layouts/default.html', {title: '列检作业记录日志', libs: ['dataGrid']}){ %>
<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-notebook"></i> ${text('列检作业记录日志')}
            </div>
            <div class="box-tools pull-right">
                <a href="#" class="btn btn-default" id="btnSearch" title="${text('查询')}"><i class="fa fa-filter"></i> ${text('查询')}</a>
                <a href="#" class="btn btn-default" id="btnSetting" title="${text('设置')}"><i class="fa fa-navicon"></i></a>
            </div>
        </div>
        <div class="box-body">
            <#form:form id="searchForm2" model="${tblXdLjzyjl}" action="${ctx}/tbl/tblTrainLog/listData" method="post" class="form-inline"
            data-page-no="${parameter.pageNo}" data-page-size="999" data-order-by="${parameter.orderBy}">
            <div class="form-group">
                <label class="control-label">${text('开始时间')}：</label>
                <div class="control-inline">
                    <#form:input path="startTime" maxlength="20" class="form-control laydate" style="width:180px !important"
                    dataFormat="date" data-type="datetime" autocomplete="off" data-format="yyyy-MM-dd HH:mm:ss" id="startTime" />
                </div>
            </div>

            <div class="form-group">
                <label class="control-label">${text('结束时间')}：</label>
                <div class="control-inline">
                    <#form:input path="endTime" maxlength="20" class="form-control laydate" style="width:180px !important"
                    dataFormat="date" data-type="datetime" autocomplete="off" data-format="yyyy-MM-dd HH:mm:ss" id="endTime" />
                </div>
            </div>

            <div class="form-group">
                <label class="control-label">${text('检别')}：</label>
                <div class="control-inline width-120">
                    <% var jbList = [{label:'列检',value:'1'},{label:'动检',value:'2'}]; %>
                    <#form:select path="typeCode" items="${jbList}" itemLabel="label" itemValue="value" class="form-control"/>
                </div>
            </div>

            <div class="form-group">
                <label class="control-label">${text('车站')}：</label>
                <div class="control-inline width-120">
                    <#form:select path="stationCode" maxlength="2" items="${lineList}" itemLabel="stationName" itemValue="stationCode" class="form-control"/>
                </div>
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-sm">${text('查询')}</button>
                <button type="reset" class="btn btn-default btn-sm">${text('重置')}</button>
            </div>


        </#form:form>

        <#form:form id="searchForm" model="${tblXdJxchsd}" action="${ctx}/tbl/replyform/tblXdJxchsd/listData5" method="post" class="form-inline"
            data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
            <#form:hidden path="recId"/>
        </#form:form>
        <table id="dataGrid2"></table>
        <table id="dataGrid"></table>

        <div id="dataGridPage"></div>
        <div id="comnfirmBox" data-url="${ctx}/tbl/tblXdLjzyjl/form" style="display: none">
            <div id="finishBox" data-url="${ctx}/tbl/tblXdLjzyjl/form2" style="display: none">
            </div>
        </div>
    </div>
</div>
    <% } %>
    <script>
        $(function(){
            // 定义一个函数来更新班次和日期
            function updateShiftAndTime() {
                var $startTime = $("#startTime");
                var $endTime = $("#endTime");
                var date_now = new Date();
                var hours = date_now.getHours(); //获取小时

                var startyear = date_now.getFullYear();  // 获取年
                var endyear = date_now.getFullYear();  // 获取年
                var startmonth = date_now.getMonth() + 1;  // 获取月
                var endmonth = date_now.getMonth() + 1;  // 获取月

                var startDay = date_now.getDate();
                var endDay = date_now.getDate();

                var startNow = '08:00:00'
                var endNow = '19:00:00'

                //设置时间回显
                if (hours >= 19) {
                    // 夜班 (19:00到0:00)，开始日期不变 时间改为19：00：00 结束日期加一 时间为08：00：00
                    date_now.setDate(date_now.getDate() + 1);// 日期加一天
                    endDay = date_now.getDate();  // 更新日
                    endmonth = date_now.getMonth() + 1;  // 更新月
                    endyear = date_now.getFullYear();  // 更新年
                    startNow = '19:00:00'
                    endNow = '08:00:00'
                } else if (hours < 8) {
                    // 夜班 (0:00到8:00)，开始日期减一 时间为19：00：00 结束日期不变 时间为 08：00：00
                    date_now.setDate(date_now.getDate() - 1); // 日期减去一天
                    startDay = date_now.getDate();  // 更新日
                    startmonth = date_now.getMonth() + 1;  // 更新月
                    startyear = date_now.getFullYear();  // 更新年
                    startNow = '19:00:00'
                    endNow = '08:00:00'
                } else {
                    // 白班 (8:00到19:00) 开始日期不变 时间为08：00：00 结束日期不变 时间为19：00：00
                }
                // 设置日期，补零处理
                $startTime.val(startyear + "-" + (startmonth < 10 ? '0' + startmonth : startmonth) + "-" + (startDay < 10 ? '0' + startDay : startDay) + " " + startNow);
                $endTime.val(endyear + "-" + (endmonth < 10 ? '0' + endmonth : endmonth) + "-" + (endDay < 10 ? '0' + endDay : endDay) + " " + endNow);
            }

            // 页面加载时更新一次
            updateShiftAndTime();
            // 每隔一分钟更新一次班次和日期
            //setInterval(updateShiftAndTime, 60000);
        });
        <!-- 第二个表格 -->

        $('#dataGrid2').dataGrid({
            searchForm: $("#searchForm2"),
            multiselect: false,  // 单选模式
            rownumWidth: 40,
            shrinkToFit: false,
            columnModel: [
                {header: '${text("线路")}', name: 'line', index: 'b.line', width: 150, align:"center"},
                {header: '${text("车数")}', name: 'wagonNum', index: 'b.wagonNum', width: 150, align:"center"},
                {
                    header: '${text("班别")}',
                    name: 'shift',
                    index: 'b.shift',
                    width: 150,
                    align: "center",
                    formatter: function(val, obj, row, act){
                        return js.getDictLabel(${@DictUtils.getDictListJson('shift_type')}, val, '${text("未知")}');
                    }
                },
                {header: '${text("组别")}', name: 'team', index: 'b.team', width: 150, align:"center"},
                {header: '${text("车站发起人")}', name: 'stationUsername', index: 'b.station_username', width: 150, align:"center"},
                {header: '${text("车站发起时间")}', name: 'stationTime', index: 'b.station_time', width: 180, align: "center"},
                {header:'${text("确认人")}', name:'confirmUsername', index:'a.confirm_username', width:150, align:"center"},
                {header:'${text("确认时间")}', name:'confirmTime', index:'a.confirm_time', width:180, align:"center"},
                {header:'${text("取消人")}', name:'cancelUsername', index:'a.cancel_username', width:150, align:"center"},
                {header:'${text("取消时间")}', name:'cancelTime', index:'a.cancel_time', width:180, align:"center"},
                {header:'${text("结束人")}', name:'finishUsername', index:'a.finish_username', width:150, align:"center"},
                {header:'${text("结束时间")}', name:'finishTime', index:'a.finish_time', width:180, align:"center"},
            ],
            onSelectRow: function(rowId, status, e) {
                // 取消已选中的行
                var selectedRows = $('#dataGrid2').getGridParam('selarrrow');
                if (selectedRows.length > 1) {
                    for (var i = 0; i < selectedRows.length; i++) {
                        if (selectedRows[i] != rowId) {
                            $('#dataGrid2').jqGrid('resetSelection', selectedRows[i]);
                        }
                    }
                }
                $("#recId").val(rowId);
                $("#searchForm").submit();
            },
            // loadComplete: function() {
            //     // 获取表格数据
            //     var gridData = $('#dataGrid2').getRowData();
            //
            //     // 批量修改 shift 列
            //     for (var i = 0; i < gridData.length; i++) {
            //         var shiftValue = gridData[i].shift;
            //         if (shiftValue == 1) {
            //             gridData[i].shift = '白班';
            //         } else if (shiftValue == 2) {
            //             gridData[i].shift = '夜班';
            //         }
            //     }
            //
            //     // 遍历每一行并更新数据
            //     for (var i = 0; i < gridData.length; i++) {
            //         $('#dataGrid2').setRowData(gridData[i].id, gridData[i]);
            //     }
            // }
        });



        // 初始化DataGrid对象
        $('#dataGrid').dataGrid({
            searchForm: $("#searchForm"),
            multiselect:true,
            rownumWidth:40,
            shrinkToFit:false,
            columnModel: [
                {header:'${text("运用号")}', name:'wagonno', index:'a.wagonno', width:150, align:"center", frozen:true},
                {header:'${text("入修地点")}', name:'repairLineName', index:'a.repair_line_name', width:150, align:"center"},
                {header:'${text("扣车原因")}', name:'faultName', index:'a.fault_name', width:150, align:"center"},
                {header:'${text("入修类别")}', name:'repairMethod', index:'a.repair_method', width:150, align:"center"},
                {header:'${text("入修方式")}', name:'repairMethodDetail', index:'a.repair_method_detail', width:150, align:"center"},
                {header:'${text("上峰")}', name:'isNopeak', index:'a.is_nopeak', width:150, align:"center", formatter: function(val, obj, row, act){
                        return js.getDictLabel(${@DictUtils.getDictListJson('sys_yes_no')}, val, '${text("未知")}', true);
                    }},
                {header:'${text("限速")}', name:'speedLimit', index:'a.speed_limit', width:150, align:"center"},
                {header:'${text("通知时间")}', name:'stationTime', index:'a.station_time', width:180, align:"center"},
                {header:'${text("办理时间")}', name:'workStartTime', index:'a.work_start_time', width:180, align:"center"},
                {header:'${text("列检员")}', name:'ljUsername', index:'a.lj_username', width:150, align:"center"},
                {header:'${text("检车人")}', name:'jcrName', index:'a.jcr_name', width:150, align:"center"},
                {header:'${text("列检调度")}', name:'ljddUsername', index:'a.ljdd_username', width:150, align:"center"},
                {header:'${text("车站调度")}', name:'czddUsername', index:'a.czdd_username', width:150, align:"center"},
                {header:'${text("更新人")}', name:'userName', index:'a.user_name', width:150, align:"center"},
                {header:'${text("更新时间")}', name:'updTime', index:'a.upd_time', width:180, align:"center"},
                {header:'${text("备注")}', name:'remark', index:'a.remark', width:150, align:"center"}

            ],

            // 加载成功后执行事件
            ajaxSuccess: function(data){

            },
            //数据加载前执行方法
            ajaxLoad: function(data){

            },
            onSelectRow: function(id, isSelect, event){
                var data = $('#dataGrid').dataGrid('getRowData', id);
                var status = data.status;
                if(status == "已确认"){
                    // class="invisible" style="width:30px"
                    $('#czp1').addClass('invisible').css('width','30px');
                    $('#czp2').removeClass('invisible').css('width','auto');
                }else{
                    $('#czp2').addClass('invisible').css('width','30px');
                    $('#czp1').removeClass('invisible').css('width','auto');
                }
            },
        }).dataGrid('setLabel', 'rn', '序号');



        //确认
        function confirm(){
            var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
            if(ids.length>0){
                if(ids.length!=1){
                    js.showMessage('不允许同时多条记录确认。', '', 'warning', 4000);
                    return;
                }else{
                    var rowData = $('#dataGrid').dataGrid('getRowData', ids[0]);
                    if(rowData.status != "未确认"){
                        js.showMessage('列检作业状态不是未确认，无法进行确认操作。', '', 'warning', 4000);
                        return;
                    }

                    $('#comnfirmBox').show();
                    js.layer.open({
                        type: 2,
                        title:['列检确认','background-color:#e2e1e1;'],
                        align:'center',
                        area: ['842px', '380px'], //宽高
                        content: $('#comnfirmBox').attr('data-url')+"?recId="+ids[0],
                        shade: 0.5 ,
                        resize:false,//弹窗不可拖拽
                        end : function(){
                            $('#comnfirmBox').hide();
                            $('#dataGrid').trigger("reloadGrid");
                        }
                    });
                }
            }else{
                js.showMessage('请选择一条记录。', '', 'warning', 4000);
                return;
            }
        }

        //结束
        function removeSc(){
            var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
            if(ids.length>0){
                if(ids.length!=1){
                    js.showMessage('不允许同时多条记录结束。', '', 'warning', 4000);
                    return;
                }else{
                    var rowData = $('#dataGrid').dataGrid('getRowData', ids[0]);
                    if(rowData.status != "已确认"){
                        js.showMessage('列检作业状态不是已确认，无法结束。', '', 'warning', 4000);
                        return;
                    }
                    $('#finishBox').show();
                    js.layer.open({
                        type: 2,
                        title:['列检结束','background-color:#e2e1e1;'],
                        align:'center',
                        area: ['842px', '330px'], //宽高
                        content: $('#finishBox').attr('data-url')+"?recId="+ids[0],
                        shade: 0.5 ,
                        resize:false,//弹窗不可拖拽
                        end : function(){
                            $('#finishBox').hide();
                            $('#dataGrid').trigger("reloadGrid");
                        }
                    });
                }
            }else{
                js.showMessage('请选择一条记录。', '', 'warning', 4000);
                return;
            }
        }

        //取消
        function cancel(){
            var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
            if(ids.length>0){
                if(ids.length!=1){
                    js.showMessage('不允许同时多条记录结束。', '', 'warning', 4000);
                    return;
                }else{
                    var msg = "确认进行【取消】操作？";
                    js.confirm(msg,function(data){
                        var rowData = $('#dataGrid').dataGrid('getRowData', ids[0]);
                        console.log(rowData.status);
                        if(rowData.status == "已确认"){
                            js.showMessage('列检作业状态已确认，无法取消。', '', 'warning', 4000);
                            return;
                        }
                        if(rowData.status == "已取消"){
                            js.showMessage('列检作业状态已取消，不要重复操作。', '', 'warning', 4000);
                            return;
                        }
                        $.post("${ctx}/tbl/tblXdLjzyjl/cancel",{'recId':ids[0]},function(e){
                            if(e.result=true){
                                js.showMessage(e.message, '', 'success', 4000);
                                $('#dataGrid').trigger("reloadGrid");
                            }else {
                                js.showMessage(e.message, '', 'warning', 4000);
                            }
                        },"json");
                    });
                }
            }else{
                js.showMessage('请选择一条记录。', '', 'warning', 4000);
                return;
            }
        }
    </script>
<style>
    .laydate-btns-clear{
        display: none !important;
    }
</style>